﻿using IRepositoryBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RepositoryCommon;
using RepositoryEntity;
using RepositoryEntity.Extension;
using RepositorySystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepositoryAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private IUserInfoBll _userInfoBll;

        public LoginController(IUserInfoBll userInfoBll)
        {
            _userInfoBll = userInfoBll;
        }

        /// <summary>
        /// 密码登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login([FromForm] string account, [FromForm] string passWord)
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            //参数认证
            if (string.IsNullOrEmpty(account))
            {
                new ContentResult() { Content = "账号不能为空" };
                result = new JsonResult(res);

                return result;
            }
            else if (string.IsNullOrEmpty(passWord))
            {

                new ContentResult() { Content = "密码不能为空" };
                result = new JsonResult(res);
                return result;

            }
            //判断账户密码是否为空
            string md5 = MD5Help.md5(passWord);//MD5加密
            UserInfo userInfo = _userInfoBll.GetUserInfoByLogin(account, md5);
            if (userInfo == null)
            {
                res.Msg = "账户或密码错误";
                result = new JsonResult(res);
                return result;

            }
            else
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "登录成功";
                res.Data = new
                {
                    UserName = userInfo.UserName
                };
                ////序列化用户对象
                string jsonUserInfo = JsonConvert.SerializeObject(userInfo);//SerializeObject:返回对象
                ////保存用户信息到session中
                HttpContext.Session.SetString("UserInfo", jsonUserInfo);
                result = new JsonResult(res);
                return result;


            }

        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult OutLogin()
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result; ;
            //删除当前sessionID
            HttpContext.Session.Remove("UserInfo");
            res.IsSuccess = true;
            res.Msg = "退出成功";
            res.Status = 1;
            result = new JsonResult(res);
            return result;
        }

        /// <summary>
        /// 判断是否为超级管理员
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult IsAdmin()
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);

            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo != null)
            {
                if (userInfo.IsAdmin)
                {
                    res.IsSuccess = true;
                    res.Status = 1;
                    res.Msg = "该账号为超级管理员";
                }
            }

            return result;
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EditPassword(string oldPassword, string newPassword)
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);
            if (string.IsNullOrEmpty(oldPassword) || string.IsNullOrEmpty(newPassword))
            {
                res.Msg = "新、旧密码不能为空";
                return result;
            }
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                res.Msg = "当前用户未获取";
                return result;
            }

            //密码加密
            string oldPwdMD5 = MD5Help.md5(oldPassword);
            string newPwdMD5 = MD5Help.md5(newPassword);
            string msg = "";
            bool isSuccess = _userInfoBll.EditPassword(oldPwdMD5, newPwdMD5, userInfo, out msg);

            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
                //删除当前登录用户的session
                HttpContext.Session.Remove("UserInfo");
            }

            res.Msg = msg;
            return result;
        }
    }
}
